<template>
  <div class="heatStation">
    <search-form
      ref="header"
      :search-data="ruleForm"
      :search-form="searchData"
      :search-handle="searchHandle"
    ></search-form>
    <base-table
      :tableData="tableData"
      :tableColumn="tableTitle"
      :isPage="false"
      :height="tableHeight"
      :tableOption="tableOption"
      @action="handleButton"
    >
      <template v-slot:label="slotData">
        <span>
          <el-tag
            v-for="tag in slotData.data.row.label"
            :key="tag"
            :type="tag"
            style="margin:0 5px 5px;"
          >
            {{ tag }}
          </el-tag>
        </span>
      </template>
    </base-table>

    <!-- 添加或编辑弹框 -->
    <addOrEidtDialog
      ref="heatStationDialog"
      v-if="dialogObj.show"
      :Visible="dialogObj.show"
      :type="dialogObj.type"
      :dialogRow="dialogObj.dialogRow"
      @close="dialogObj.show = false"
      @refresh="getHeatStationData"
    ></addOrEidtDialog>
  </div>
</template>
<script>
import baseTable from "@/components/Table/index.vue";
import { onMounted, reactive, toRefs } from "vue";
import searchForm from "@/components/SearchForm/index.vue";
import addOrEidtDialog from "./component/addOrEditDialog.vue";
import { useStore } from "vuex";

import { getHeatStation, delHeatStation } from "@/api/heatStation";
import { ElMessage, ElMessageBox } from "element-plus";

import heightMinxin from "@/utils/tableHeight.mixin";
export default {
  name: "heatStation",
  mixins: [heightMinxin],
  components: { baseTable, addOrEidtDialog, searchForm },
  setup() {
    const store = useStore();
    const state = reactive({
      ruleForm: { heatSourceId: "" },
      searchData: [
        {
          type: "Select",
          prop: "heatSourceId",
          options: [],
          placeholder: "选择热源",
          change: () => {
            // getHeatStationData();
          },
        },
      ],
      searchHandle: [
        {
          label: "搜索",
          type: "primary",
          handle: () => {
            getHeatStationData();
          },
        },
        {
          label: "添加",
          handle: () => {
            const { heatSourceId } = state.ruleForm;
            if (heatSourceId == "") {
              return ElMessage.error("热源不能为空");
            }
            let heatArr = state.searchData[0].options.filter(
              (item) => item.value === heatSourceId
            );

            handleAdd("add", {
              heatSourceId,
              heatSourceName: heatArr[0].label,
            });
          },
        },
      ],
      tableData: [],
      tableTitle: [
        { prop: "heatSourceName", label: "热源名称" },
        { prop: "name", label: "换热站名称" },
        { prop: "code", label: "换热站编码" },
        { prop: "shortName", label: "简称" },
        {
          prop: "longitude",
          label: "经度",
        },
        { prop: "latitude", label: "纬度" },
        {
          prop: "label",
          label: "标签",
          columnType: "slot",
          slotName: "label",
        },
        { prop: "remark", label: "备注" },
      ],
      tableOption: {
        label: "操作",
        buttons: [
          { name: "编辑", methods: "edit" },
          { name: "删除", methods: "delete", style: { color: "red" } },
        ],
      },
      dialogObj: {
        show: false,
        type: "add",
        dialogRow: {},
      },
    });

    onMounted(async () => {
      await getHeatSourceList();
      getHeatStationData();
    });
    /**获取热源列表 */
    const getHeatSourceList = async () => {
      let result = store.getters.sourceData;
      state.searchData[0].options = result;
      state.ruleForm.heatSourceId = result ? result[0].value : "";
    };
    const getHeatStationData = async () => {
      if (state.ruleForm.heatSourceId == "") {
        ElMessage.error("热源不能为空");
        return;
      }

      const { data: res } = await getHeatStation(state.ruleForm);

      if (res.success) {
        const list = res.data.map((item) => {
          item.label = item.label ? JSON.parse(item.label) : [];
          return item;
        });
        state.tableData = list;
      }
    };

    const handleAdd = (type, row) => {
      state.dialogObj.show = true;
      state.dialogObj.type = type;
      state.dialogObj.dialogRow = row;
    };

    const handleButton = ({ row, methods, index }) => {
      if (methods == "edit") {
        handleAdd(methods, row);
      } else {
        handleDelete(row, index);
      }
    };

    const handleDelete = (row, index) => {
      ElMessageBox.confirm(`确定删除该热源名称-${row.name}?`, "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          delHeatStation({ id: row.id }).then((res) => {
            if (res.data.success) {
              state.tableData.splice(index, 1);
              ElMessage.success(res.data.msg);
            }
          });
        })
        .catch(() => {});
    };
    return { ...toRefs(state), handleAdd, handleButton, getHeatStationData };
  },
};
</script>
<style lang="less"></style>
